package com.bicon.botu.file.upload;

import com.bicon.botu.core.base.annotation.EnableGlobalExceptionHandler;
import com.bicon.botu.core.base.annotation.EnableHttpLogAspect;
import com.bicon.botu.core.base.annotation.EnableSystemInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 文件上传启动类
 */
@SpringBootApplication(exclude = {MultipartAutoConfiguration.class,
        MongoAutoConfiguration.class,
        MongoDataAutoConfiguration.class,
        JpaRepositoriesAutoConfiguration.class,
        DataSourceAutoConfiguration.class})
@EnableGlobalExceptionHandler
@EnableSystemInterceptor
@EnableHttpLogAspect
public class FileUploadApplication extends WebMvcConfigurationSupport {

    public static void main(String[] args) {
        SpringApplication.run(FileUploadApplication.class, args);
    }

    @Value("${max.memory.size}")
    private Integer max_memory_size;
    @Value("${max.upload.size}")
    private Long max_upload_size;

    //显示声明CommonsMultipartResolver为mutipartResolver
    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8");
        //resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
        resolver.setResolveLazily(true);
        resolver.setMaxInMemorySize(max_memory_size);
        //上传文件大小 100M 50*1024*1024
        resolver.setMaxUploadSize(max_upload_size);
        return resolver;
    }

//    @Bean
//    public SystemInterceptor getMyInterceptor(){
//        return new SystemInterceptor();
//    }
//
//    /**
//     * 配置拦截器
//     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
//    }

}
